package com.tcm.invite.service;

import com.base.core.exception.ServiceException;
import com.base.core.pager.PageBean;
import com.base.core.pager.PageContext;
import com.base.core.utils.R;
import com.tcm.invite.domain.dto.InviteRecordDTO;
import com.tcm.invite.domain.entity.InviteRecord;
import com.tcm.invite.mapper.InviteRecordMapper;
import com.tcm.user.model.dto.UserBaseInfoDTO;
import com.tcm.user.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@DubboService(version = "0.0.1-alpha", timeout = 6000, retries = 3, delay = -1)
public class InviteRecordServiceImpl implements InviteRecordService{

    @Autowired
    private InviteRecordMapper inviteRecordMapper;


    private UserService userService;
    /**
     * 查询我的推广记录
     * @param spreadType
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public PageBean<InviteRecordDTO> queryInviteRecordPage(String cellphone, String spreadType, Long page, Long pagesize) {
        Long count = inviteRecordMapper.countBySpreadType(cellphone, spreadType);
        PageContext.setCurrentPage(page);
        PageContext.setPageSize(pagesize);
        PageContext.setDataCount(count);
        if(count.intValue() == 0){
            return new PageBean<>(null);
        }
        List<InviteRecord> inviteRecords = inviteRecordMapper.queryInviteRecordPage(cellphone, spreadType,PageContext.getStart(),pagesize);
        List<InviteRecordDTO> inviteRecordDTOList = inviteRecords.stream().map(inviteRecord -> {
            InviteRecordDTO inviteRecordDTO = new InviteRecordDTO();
            BeanUtils.copyProperties(inviteRecord,inviteRecordDTO);
            return inviteRecordDTO;
        }).collect(Collectors.toList());
        PageContext.clear();
        return new PageBean<>(inviteRecordDTOList);
    }

    @Override
    public InviteRecordDTO findByInvitedCode(String invitedCode, String spreadType){
        InviteRecord inviteRecord = inviteRecordMapper.findByInvitedCode(invitedCode, spreadType);
        InviteRecordDTO inviteRecordDTO = null;
        if(inviteRecord != null){
            inviteRecordDTO = new InviteRecordDTO();
            BeanUtils.copyProperties(inviteRecord, inviteRecordDTO);
        }
        return inviteRecordDTO;
    }

    @Override
    public List<InviteRecordDTO> queryInviterByPhone(String phone, String spreadType){
        List<InviteRecord> inviteRecordList = inviteRecordMapper.findByPhone(phone, spreadType);
        if(inviteRecordList == null){
            return null;
        }
        List<String> inviteCodes = inviteRecordList.stream().map(InviteRecord::getInvitedCode)
                .distinct().collect(Collectors.toList());
        //根据手机号码获得头像信息
        R ret = userService.queryByMobile(inviteCodes,null);
        Map<String, Object> userMaps = new HashMap<>();
        if(ret.isSuccess() && ret.getData() != null){
            List<UserBaseInfoDTO> userMapList = (List<UserBaseInfoDTO>) ret.getData();
            userMaps = userMapList.stream().collect(Collectors.toMap(
                    UserBaseInfoDTO::getMobile, Function.identity(),(key1,key2)->key2));
        }
        Map<String, Object> finalUserMaps = userMaps;
        List<InviteRecordDTO> inviteRecordDTOList = inviteRecordList.stream()
                .map(inviteRecord -> {
                    UserBaseInfoDTO userInfo = (UserBaseInfoDTO) finalUserMaps.get(inviteRecord.getInvitedCode());
                    InviteRecordDTO inviteRecordDTO = new InviteRecordDTO();
                    BeanUtils.copyProperties(inviteRecord, inviteRecordDTO);
                    if(userInfo != null){
                        inviteRecordDTO.setInvitedAvatar(userInfo.getAvatar());
                        inviteRecordDTO.setInvitedNickame(userInfo.getNickname());
                    }
                    return inviteRecordDTO;
                }).collect(Collectors.toList());
        return inviteRecordDTOList;
    }

    @Override
    public void update(InviteRecordDTO inviteRecordDTO) throws ServiceException {
        InviteRecord inviteRecord = new InviteRecord();
        BeanUtils.copyProperties(inviteRecordDTO, inviteRecord);
        inviteRecord.setUpdateTime(LocalDateTime.now());
        inviteRecordMapper.update(inviteRecord);
    }

    @Override
    public void insert(InviteRecordDTO inviteRecordDTO) throws ServiceException {
        InviteRecord inviteRecord = new InviteRecord();
        BeanUtils.copyProperties(inviteRecordDTO, inviteRecord);
        inviteRecordMapper.insert(inviteRecord);
    }


}
